package com.vsd.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.demo.dialet.PaginationSupport;
import com.demo.util.SqlUtils;
import com.demo.util.StringUtil;
import com.vsd.daoEx.model.IndexActGoodsExWithGoods;
import com.vsd.daoEx.model.IndexActStoreEx;
import com.vsd.daoEx.model.IndexActStoreExWithStore;
import com.vsd.domain.IndexActStore;
import com.vsd.repository.IndexActStoreRepository;
import com.vsd.service.IndexActStoreService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service
@Transactional
public class IndexActStoreServiceImpl implements IndexActStoreService {

	@SuppressWarnings("unused")
	private static final Logger LOGGER = LoggerFactory
			.getLogger(IndexActStoreServiceImpl.class);
	
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	@Autowired
	private IndexActStoreRepository  indexActStoreRepository;


	@Override
	public Collection<IndexActStoreEx> selectAppList(String actId,String lng,String lat, Integer page,Integer limit) {
		Collection<IndexActStoreEx> ret = new ArrayList<IndexActStoreEx>();
		String sql = "select st.id as storeId,st.`name` as storeName,st.logo,st.cate_code as cateCode"
				+ ",st.area_code as areaCode,st.avg_score as star,c.`name` as cateName,ci.`name` as areaName";
		if(StringUtils.isNoneBlank(lat)&&StringUtils.isNoneBlank(lng)){
			sql+=",getDistance(st.lng,st.lat,"+lng+","+lat+") as distance";
		}
		sql+= ",(select count(1) from s_store_comment sc where sc.store_id=st.id) as commentCnt"
		+ ",(select AVG(score) from s_store_comment sco where sco.store_id=st.id) as avgScore"
		+ "	from f_index_act_store s	left join s_store st on s.store_id = st.id"
		+ "	left join s_store_category c on c.`code` = st.cate_code"
		+ "	left join f_city ci on ci.`code` = st.area_code"
		+" where st.is_online=1 and st.closed=0 and  s.act_id = '"+actId+"'"
		+ " order by s.sort_order,s.create_time desc limit "+page*limit+","+(page+1)*limit;
		
		List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
		
		for (Map<String, Object> map : maps) {
			IndexActStoreEx e = new IndexActStoreEx();
			e.setStoreId(StringUtil.obj2String(map.get("storeId")));
			e.setStoreName(StringUtil.obj2String(map.get("storeName")));
			e.setLogo(StringUtil.obj2String(map.get("logo")));
			e.setDistance(StringUtil.obj2String(map.get("distance")));
			e.setStar(map.get("star")==null?0:new Integer(map.get("star").toString()));
			e.setCommentCnt(map.get("commentCnt")==null?0:new Integer(map.get("commentCnt").toString()));
			e.setCateCode(StringUtil.obj2String(map.get("cateCode")));
			e.setCateName(StringUtil.obj2String(map.get("cateName")));
			e.setAreaCode(StringUtil.obj2String(map.get("areaCode")));
			e.setAreaName(StringUtil.obj2String(map.get("areaName")));
			ret.add(e);
		}
		
		return ret;
	}


	@Override
	public List<IndexActStoreExWithStore> selectExList(String areaCode) {
		String sql = " select ias.*,s.name as storeName,s.logo,s.imgs,s.address,s.phone,ia.title,s.closed from f_index_act_store ias "
				+ "left join s_store s on s.id = ias.store_id "
				+ "left join f_index_activity ia on ia.id = ias.act_id "
				+ "where 1=1 ";
		
		if(StringUtils.isNotBlank(areaCode)){
			sql+=" and ia.area_code = '"+areaCode+"'";
		}
		sql+=" order by ias.create_time desc";
		
		int c = jdbcTemplate.queryForObject("select count(1) from ("+ sql+") a_cnt", Integer.class);
		PaginationSupport.getContext().setTotalCount(c);
		
		sql+=" limit "+(PaginationSupport.getContext().getStartRowNo()-1)+","+PaginationSupport.getContext().getPageLength();
		
		
		List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
		List<IndexActStoreExWithStore> exList = new ArrayList<>();
		for (Map<String, Object> map : maps) {
			IndexActStoreExWithStore ex = new IndexActStoreExWithStore();
			SqlUtils.transMap2Bean2(map, ex);
			exList.add(ex);
		}
		return exList;
	}


	@Override
	public void delById(String id) {
		// TODO Auto-generated method stub
		indexActStoreRepository.delete(id);
	}


	@Override
	public int updateByPrimaryKey(IndexActStore store) {
		try {
			String sql = SqlUtils.update(store, true, " where id = '"+store.getId()+"'");
			return jdbcTemplate.update(sql);
		} catch (  Exception e) {
			return 0;
		}
	}


	@Override
	public IndexActStore findByStoreIdAndActId(String storeId, String actId) {
		// TODO Auto-generated method stub
		return indexActStoreRepository.findByStoreIdAndActId(storeId, actId);
	}

	@Override
	public void save(IndexActStore store) {
		// TODO Auto-generated method stub
		indexActStoreRepository.save(store);
	}
 

	
}
